#ifndef __LCD4P3_C
#define __LCD4P3_C

#include "LCD4P3.h"

//写寄存器
void display_write_register8(uint8_t addr,uint8_t value){
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST;
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND;
		DISPLAY_BUFFER[2] = 0x03;   //指令长度
		DISPLAY_BUFFER[3] = WRITE_REGISTER;
		DISPLAY_BUFFER[4] = addr;
		DISPLAY_BUFFER[5] = value;

		#if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,6);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,6);	
		#endif
	
}

//写寄存器
void display_write_register16(uint8_t addr,uint16_t value){
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST;
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND;
		DISPLAY_BUFFER[2] = 0x04;   //指令长度
		DISPLAY_BUFFER[3] = WRITE_REGISTER;
		DISPLAY_BUFFER[4] = addr;
		DISPLAY_BUFFER[5] = value >> 8;
		DISPLAY_BUFFER[6] = value & 0xff;	
		#if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,7);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,7);	
		#endif
}

//写变量存储器
void display_write_variate(uint16_t addr,uint16_t value){
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST;
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND;
		DISPLAY_BUFFER[2] = 0x05; //指令长度
		DISPLAY_BUFFER[3] = WRITE_VARIATE;
		DISPLAY_BUFFER[4] = addr >> 8;
		DISPLAY_BUFFER[5] = addr & 0xff;
		DISPLAY_BUFFER[6] = value >> 8;
		DISPLAY_BUFFER[7] = value & 0xff;	 
	  #if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,8);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,8);	
		#endif
}

//写变量存储器
void display_write_arr_variate(uint16_t addr,int8_t *arrValue,uint8_t len){ 
	  uint8_t i = 0;
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST; 
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND; 
		DISPLAY_BUFFER[2] = 3 + len; //指令长度
		DISPLAY_BUFFER[3] = WRITE_VARIATE; 
		DISPLAY_BUFFER[4] = addr >> 8; 
		DISPLAY_BUFFER[5] = addr & 0xff; 	  
	  for(i = 0;i < len; i++){
			  // 高位在前:
			  DISPLAY_BUFFER[6 + i] = arrValue[i]; 
		}
	  #if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,6 + len); 	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,6 + len);	
		#endif
}

//写变量存储器
void display_write_rtc(uint16_t addrRTC){  
		DISPLAY_BUFFER[0]  = FRAME_HEADER_FIRST;  
		DISPLAY_BUFFER[1]  = FRAME_HEADER_SECOND; 
	  DISPLAY_BUFFER[2]  = 0x0A; 
		DISPLAY_BUFFER[3]  = WRITE_VARIATE; 
		DISPLAY_BUFFER[4]  = addrRTC >> 8; 
		DISPLAY_BUFFER[5]  = addrRTC & 0xff; 
	  DISPLAY_BUFFER[6]  = RTC_data[0]; 
	  DISPLAY_BUFFER[7]  = RTC_data[1]; 
	  DISPLAY_BUFFER[8]  = RTC_data[2]; 
	  DISPLAY_BUFFER[9]  = RTC_data[3]; 
	  DISPLAY_BUFFER[10] = RTC_data[4]; 
	  DISPLAY_BUFFER[11] = RTC_data[5]; 
	  DISPLAY_BUFFER[12] = RTC_data[6]; 
	  #if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,13);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,13);	
		#endif
}

// 切换页面：
void change_page(uint16_t pageId){
	 display_write_register16(PIC_ID,pageId);		 
	 timer_recoder[0]  = 0;
	 timer_recoder[1]  = 0;
	 timer_recoder[2]  = 0;
	 lcd_page_id = (uint8_t)pageId;
	 
}

// 蜂鸣器鸣叫
void lcd_beep(uint8_t beepTm){
	 display_write_register8(addr_beep,beepTm);	
}

#if beep_type == beep_LCD 
// 蜂鸣器鸣叫：beep_times_input = 鸣叫次数; beep_Tm_input = 鸣叫坚持时间；beep_idle_Tm_input = 每次鸣叫后时间间隔
void beep_start(uint8_t beep_times_input,uint16_t beep_Tm_input,uint16_t beep_idle_Tm_input){
	  beep.beep_start_tm = system_time;
	  beep.beep_step = 1;
	  beep.beep_times = beep_times_input;
	  beep.beep_Tm = beep_Tm_input;
	  beep_idle_Tm = beep_idle_Tm_input;	
}
void lcd_beep_step_run(){
	  if(system_time - beep.beep_start_tm > beep.beep_Tm + beep.beep_idle_Tm){
				lcd_beep((uint16_t)(beep.beep_Tm / 10));
			  beep.beep_start_tm = system_time;
				beep.beep_step++;
		}
}
#endif

#endif

